import cv2
import numpy as np

img = cv2.imread("./person.png")
tmp = img.copy()
h, w, c = img.shape
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lowerb = np.array([0, 43, 46])
upperb = np.array([1, 255, 255])
red_mask = cv2.inRange(hsv_img, lowerb, upperb)
# 闭运算：填充里面的孔洞
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# 开闭运算需要配合使用，否则图像边缘会走位
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_CLOSE, kernel)
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_OPEN, kernel)
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_CLOSE, kernel)
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_DILATE, kernel)

for i in range(h):
    for j in range(w):
        if red_mask[i, j] == 255:
            tmp[i, j] = (255, 0, 0)

cv2.imshow("person", tmp)
cv2.waitKey(0)
